/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gu.derp.beans.account;

import edu.gu.derp.model.Account;
import edu.gu.derp.model.Database;
import edu.gu.derp.model.IController;
import edu.gu.derp.model.Mail;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;

/**
 *
 * @author jensc
 */
@ManagedBean(name = "AccountControllerBean")
@RequestScoped
public class AccountControllerBean {

    private final int CONSTRAINT_PASSWORD_MIN_LENGTH = 5;
    private final int CONSTRAINT_PASSWORD_MAX_LENGTH = 20;
    
    private final String ERROR_PASSWORD_TO_SHORT = "The password is to short it should be atleast " + CONSTRAINT_PASSWORD_MIN_LENGTH + "characters long";
    private final String ERROR_PASSWORD_TO_LONG = "The password is to long it should be no more then " + CONSTRAINT_PASSWORD_MAX_LENGTH + " characters long";
    private final String ERROR_PASSWORD_DONT_MATCH = "The password entered don't match each other";
    private final String ERROR_OLD_PASSWORD_DONT_MATCH = "The password entered don't match with your old password";
   
    private final String ACTION_REMOVE_ACCOUNT = "The Account is removed";
    
    @ManagedProperty(value = "#{AccountBackingBean.oldPassword}")
    private String oldPassword;
    @ManagedProperty(value = "#{AccountBackingBean.newPassword}")
    private String newPassword;
    @ManagedProperty(value = "#{AccountBackingBean.confirmPassword}")
    private String confirmPassword;
    private String accountName;
    
    public AccountControllerBean(){
        setName();
    }

    public void changePassword() {
        FacesContext context = FacesContext.getCurrentInstance();

        IController controller = Database.getController();

        Account account = (Account) context.getExternalContext().getSessionMap().get("account");

        // Error messages if the constraints isn't met
        if (!oldPassword.equals(account.getPassword())) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_OLD_PASSWORD_DONT_MATCH);
            message.setDetail(ERROR_OLD_PASSWORD_DONT_MATCH);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formAccount:oldPassword", message);
        }

        if (newPassword.length() < CONSTRAINT_PASSWORD_MIN_LENGTH) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_TO_SHORT);
            message.setDetail(ERROR_PASSWORD_TO_SHORT);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formAccount:newPassword", message);
        } else if (newPassword.length() > CONSTRAINT_PASSWORD_MAX_LENGTH) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_TO_LONG);
            message.setDetail(ERROR_PASSWORD_TO_LONG);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formAccount:newPassword", message);
        }

        if (!newPassword.equals(confirmPassword)) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_DONT_MATCH);
            message.setDetail(ERROR_PASSWORD_DONT_MATCH);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formAccount:confirmPassword", message);

        } else {
            controller.changePassword(account, newPassword);

        }


    }

    public String removeAccount() {

        FacesContext context = FacesContext.getCurrentInstance();
        IController controller = Database.getController();
        Account account = (Account) context.getExternalContext().getSessionMap().get("account");

        controller.removeAccount(account);
        
        FacesMessage message = new FacesMessage();
        message.setSummary(ACTION_REMOVE_ACCOUNT);
        message.setDetail(ACTION_REMOVE_ACCOUNT);
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage("formLogin:name", message);
        
        context.getExternalContext().invalidateSession();
        try {
            Mail.sendUnregMail(account);
        } catch (MessagingException ex) {
            Logger.getLogger(AccountControllerBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "login.jsf";

    }
    
    private void setName(){
        
        FacesContext context = FacesContext.getCurrentInstance();    
        IController controller = Database.getController();
        Account account = (Account) context.getExternalContext().getSessionMap().get("account");
       
        accountName = account.getUsername();
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }
    
    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }
}
